home *** CD-ROM | disk | FTP | other *** search
- unit Fp1;
- { PC Plus sample Delphi program.
- This program shows how to call procedures and functions
- and illustrates the difference between value parameters
- and VAR parameters }
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Edit2: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- Button1: TButton;
- Button2: TButton;
- Button3: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- { PROC1 PROCEDURE WITH VALUE PARAMETER }
- { Here the s parameter is passed 'by value'. In other words,
- it is a copy of the original string. Any alterations made
- to this copy, won't alter the original. So, in this case,
- when the copy is set to LowerCase, the original remains
- unchanged }
- procedure proc1( s : string );
- begin
- s := LowerCase( s );
- end;
-
- { PROC2 PROCEDURE WITH VAR PARAMETER }
- { Here s is a 'variable parameter'. This means it points to }
- { (in effect, it *is*) the original string. When you change }
- { a VAR parameter, the original string is changed too. In }
- { this case, both the parameter, s, and the string in the }
- { calling procedure will be changed to lower case. }
- procedure proc2( var s : string );
- begin
- s := LowerCase( s );
- end;
-
-
- { FUNCT1 A FUNCTION RETURNING A STRING }
- { Functions return a value of the type declared after the }
- { colon in the Function heading - here ' : string;' }
- { The value to be returned can be assigned to the Function }
- { name itself - here 'Func1 :=' }
- { As with a procedure, the parameter, 's', is left unchanged }
- { if it is a value paramater but it would be changed if it }
- { were a VAR parameter. }
- { This function leaves s unchanged but returns a LowerCase }
- { copy of s. }
- function Func1( s : string ) : string;
- begin
- Func1 := LowerCase( s );
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- s : string;
- begin
- s := Edit1.Text;
- proc1(s);
- Edit2.Text := s;
- end;
-
- procedure TForm1.Button2Click(Sender: TObject);
- var
- s : string;
- begin
- s := Edit1.Text;
- proc2(s);
- Edit2.Text := s;
- end;
-
- procedure TForm1.Button3Click(Sender: TObject);
- begin
- Edit2.Text := Func1( Edit1.Text );
- end;
-
- end.
-